<?php
namespace app\backend\controller;
use think\Db;
use think\Request;
use think\Validate;
use think\Controller;

class Login extends Controller
{
     public function login()
    {
        return view('index/login');
    }
    public function check()
    {
    	session_start();
		$admin_name = input('post.admin_name','','strip_tags');
		$admin_pwd = input('post.admin_pwd');
		$admin_mpwd=md5($admin_pwd);
		$verify=input('verify');
		
		if(empty($verify))
            {
            	$this->error("验证码不能为空");
            }
        else if(!captcha_check($verify))
            {
            	$this->error("验证码错误");
            }

		
		// 密码要使用密文传输,项目后期再设置
		// $user_pwd = md5($user_pwd);

		$ret = db('admin')->where("admin_name = '{$admin_name}' and admin_pwd = '{$admin_mpwd}'")->find();
		if($ret == false){
			$this->error("账号不匹配或密码不匹配");
		}
		
		session('admin_name',$ret['admin_name']);
		session('admin_id',$ret['admin_id']);

		  $updata=[
	        'admin_ip'=>request()->ip(),
	        'admin_logintime'=>time()             
	      ];


      
     
       $ret=db('admin')->where("admin_name='{$admin_name}'")->update($updata);

		$this->success("登录成功",url('/backend/Index/index'));
	}

	public function logout(){
            session(null);
            return $this->success('退出成功',url('/backend/index/index'));
        }
}
